package com.prs.crm.action.oa.report;

import java.util.Collection;
import java.util.Date;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.action.ReportAction;
import com.prs.crm.context.Constrains;
import com.prs.crm.context.ToolKits;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.oa.OaTask;
import com.prs.crm.report.ChartResult;
import com.prs.crm.report.ChartService;
import com.prs.crm.report.ExportConfig;
import com.prs.crm.report.ReportCell;
import com.prs.crm.report.ReportTable;
import com.prs.crm.service.oa.OaTaskService;

public class TaskAnalysisAction extends ReportAction {

	private static final long serialVersionUID = 6443684980622011589L;

	private String state;
	private ChartService chartService;
	private OaTaskService oaTaskService;
	private ChartResult chartResult;
	private PaginationSupport pagination;
	private Date datestart;
	private Date dateend;
	private String accepts;
	private String acceptnames;
	private String category;
	private int exportType;
	private Person person;

	@Action("taskAnalysis")
	public String taskAnalysis() {
		return SUCCESS;
	}

	@Action(value = "viewTaskPie", results = {
			@Result(name = "success", type = "chain", location = "taskAnalysis"),
			@Result(name = "error", type = "chain", location = "taskAnalysis") })
	public String viewTaskPie() {
		if (state == null || "".equals(state.trim())) {
			addActionError("状态不能为空");
			return ERROR;
		}
		if (datestart == null) {
			addActionError("开始时间不能为空");
			return ERROR;
		}
		if (dateend == null) {
			addActionError("结束时间不能为空");
			return ERROR;
		}
		if (accepts == null || accepts.trim().equals("")) {
			addActionError("人不能为空员");
			return ERROR;
		}
		Collection<Object[]> dataList = oaTaskService.getTaskReportDataSouce(
				state, datestart, dateend,
				!accepts.trim().equals("") ? chartService
						.convertIdsToPersons(ToolKits
								.convertStringAryToIntAry(accepts.split(",")))
						: null);
		chartResult = chartService.export(chartService.fillDataset(dataList,
				Constrains.REPORT_PIECHART), "任务分析", "taskInfo");
		return SUCCESS;
	}

	@Action(value = "viewTaskBar", results = {
			@Result(name = "success", type = "chain", location = "taskAnalysis"),
			@Result(name = "error", type = "chain", location = "taskAnalysis") })
	public String viewTaskBar() {
		if (state == null || "".equals(state.trim())) {
			addActionError("状态不能为空");
			return ERROR;
		}
		if (datestart == null) {
			addActionError("开始时间不能为空");
			return ERROR;
		}
		if (dateend == null) {
			addActionError("结束时间不能为空");
			return ERROR;
		}
		if (accepts == null || accepts.trim().equals("")) {
			addActionError("人员");
			return ERROR;
		}
		Collection<Object[]> dataList = oaTaskService.getTaskReportDataSouce(
				state, datestart, dateend,
				!accepts.trim().equals("") ? chartService
						.convertIdsToPersons(ToolKits
								.convertStringAryToIntAry(accepts.split(",")))
						: null);
		chartResult = chartService.export(chartService.fillDataset(dataList,
				Constrains.REPORT_BARCHART), "任务分析", "次数", "", "taskInfo");
		return SUCCESS;
	}

	@Action("taskInfo")
	public String taskInfo() {
		person = chartService.getPerson(this.getCategory());
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				OaTask.class);
		if (state != null)
			criteria.add(Restrictions.eq("state", state));
		if (datestart != null)
			criteria.add(Restrictions.ge("beginTime", datestart));
		if (dateend != null)
			criteria.add(Restrictions.le("beginTime", dateend));
		if (person != null)
			criteria.add(Restrictions.eq("recorder", person));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getOaTaskService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action(value = "exportTaskReport", results = { @Result(name = "success", type = "stream", params = {
			"contentType", "${contentType}", "inputName", "inputStream",
			"bufferSize", "1024", "contentDisposition",
			"filename=${outfileName}" }) })
	public String exportTaskReport() {
		ReportTable table = new ReportTable("任务分析报表");
		table.addCell(new ReportCell("序号", "id"));
		table.addCell(new ReportCell("任务名称", "title"));
		table.addCell(new ReportCell("任务类型 ", "type.name"));
		table.addCell(new ReportCell("任务来源", "source.name"));
		table.addCell(new ReportCell("创建人", "recorder.resume.name"));
		table.addCell(new ReportCell("任务级别", "level"));
		table.addCell(new ReportCell("开始时间", "beginTime"));
		table.addCell(new ReportCell("结束时间", "endTime"));
		table.addCell(new ReportCell("状态", "state", Constrains
				.getReportTaskStatus()));
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				OaTask.class);
		if (state != null)
			criteria.add(Restrictions.eq("state", state));
		if (datestart != null)
			criteria.add(Restrictions.ge("beginTime", datestart));
		if (dateend != null)
			criteria.add(Restrictions.le("beginTime", dateend));
		if (person != null)
			criteria.add(Restrictions.eq("recorder", person));
		this.setOutFilePath(this.getReportService().report(criteria, table,
				new ExportConfig(), this.getExportType()));
		return SUCCESS;
	}

	public String getState() {
		return state;
	}

	public ChartService getChartService() {
		return chartService;
	}

	public OaTaskService getOaTaskService() {
		return oaTaskService;
	}

	public ChartResult getChartResult() {
		return chartResult;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public Date getDatestart() {
		return datestart;
	}

	public Date getDateend() {
		return dateend;
	}

	public String getAccepts() {
		return accepts;
	}

	public String getAcceptnames() {
		return acceptnames;
	}

	public String getCategory() {
		return category;
	}

	public int getExportType() {
		return exportType;
	}

	public Person getPerson() {
		return person;
	}

	public void setState(String state) {
		this.state = state;
	}

	public void setChartService(ChartService chartService) {
		this.chartService = chartService;
	}

	public void setOaTaskService(OaTaskService oaTaskService) {
		this.oaTaskService = oaTaskService;
	}

	public void setChartResult(ChartResult chartResult) {
		this.chartResult = chartResult;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public void setDatestart(Date datestart) {
		this.datestart = datestart;
	}

	public void setDateend(Date dateend) {
		this.dateend = dateend;
	}

	public void setAccepts(String accepts) {
		this.accepts = accepts;
	}

	public void setAcceptnames(String acceptnames) {
		this.acceptnames = acceptnames;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public void setExportType(int exportType) {
		this.exportType = exportType;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

}
